﻿Imports System.IO.Ports
Imports System.Threading

Public Class Form3

    Dim DataIn As Char
    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


    Private Function SendCh(ByVal data As Char) As Char
        If Form1.PadEvents.IsOpen Then
            Form1.PadEvents.Write(data)
            'wait until the data is sended
            Thread.Sleep(10)
            Form1.PadEvents.ReadTimeout = 100
            'save answer from com
            DataIn = (Form1.PadEvents.ReadTo(data))
            'Form1.PadEvents.Read(rdata)
            MsgBox("" & DataIn.ToString)
            Return DataIn
        Else
            Return Chr(0)
        End If

    End Function

    Public Function ReadPad(ByVal count As Integer) As Byte()
        Dim buffer(count - 1) As Byte
        Dim readBytes As Integer
        Dim totalReadBytes As Integer
        Dim offset As Integer
        Dim remaining As Integer = count

        Try
            Do
                readBytes = Form1.PadEvents.Read(buffer, offset, remaining)
                offset += readBytes
                remaining -= readBytes
                totalReadBytes += readBytes
            Loop While remaining > 0 AndAlso readBytes > 0
        Catch ex As TimeoutException
            ReDim Preserve buffer(totalReadBytes - 1)
        End Try

        Return buffer
    End Function

    Public Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'Check if  port is open and then close it
        If Form1.PadEvents.IsOpen Then
            Form1.PadEvents.Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cad(0 To 10) As Byte
        Dim DATA As String

        Label1.Text = "CONNECTING TO PAD"
        Label4.Text = "OPENING PORT"
        If Not (Form1.PadEvents.IsOpen) Then
            Form1.PadEvents.PortName = Form1.Portname
            Form1.PadEvents.Open()
            If (Form1.PadEvents.IsOpen) Then
                ProgressBar1.Value += 20
            Else
                MsgBox("CANT NO OPEN THE PORT")
                Me.Close()
            End If
        End If
        Label4.Text = "PRESS A"
        Do While DATA <> "A"
            cad = ReadPad(1)
            DATA = System.Text.Encoding.Default.GetString(cad)
        Loop
        ProgressBar1.Value += 27

        Label4.Text = "PRESS UP"
        Do While DATA <> "U"
            cad = ReadPad(1)
            DATA = System.Text.Encoding.Default.GetString(cad)
        Loop
        ProgressBar1.Value += 27

        Label4.Text = "PRESS START"
        Do While DATA <> "T"
            cad = ReadPad(1)
            DATA = System.Text.Encoding.Default.GetString(cad)
        Loop
        ProgressBar1.Value = 100
        Form1.ConnectioStatus = 1
        MsgBox("THANK YOU, NOW LET'S PLAY")
        Me.Close()

    End Sub
End Class